package com.leo.ktvplayer.localcookie

import android.preference.PreferenceManager
import com.leo.ktvplayer.base.App
import com.leo.ktvplayer.domain.bean.Setting
import com.leo.ktvplayer.domain.bean.User
import com.leo.ktvplayer.util.JsonUtil

/**
 * Created by LeoLu on 2017/6/9.
 */
object LocalUtilsByKotlin {
    private val TAG_USER = "com.leo.ktvplayer.TAG_USER"
    private val TAG_SETTING = "com.leo.ktvplayer.TAG_SETTING"

    fun getUser() : User{
        var result : String? = PreferenceManager.getDefaultSharedPreferences(App.getInstance()).getString(TAG_USER, null);
        if(result == null) {
            return User();
        }
        return JsonUtil.jsonStrToObj(User::class.java,result);
    }

    fun saveUser(user : User?){
        if (user == null) {
            var result : String = JsonUtil.objToJsonStr(user);
            PreferenceManager.getDefaultSharedPreferences(App.getInstance()).edit().putString(TAG_USER, result).commit()
        }
    }

    fun getSetting():Setting{
        var result : String = PreferenceManager.getDefaultSharedPreferences(App.getInstance()).getString(TAG_SETTING,null);
        if (result != null){
            return JsonUtil.jsonStrToObj(Setting::class.java,result)
        } else {
            return Setting()
        }
    }

    fun saveSetting(setting: Setting?){
        if (setting != null){
            val result = JsonUtil.objToJsonStr(setting)
            PreferenceManager.getDefaultSharedPreferences(App.getInstance()).edit().putString(TAG_SETTING, result).commit()
        }
    }

}